package de.tabuma.projectview.cssdropdown;

import java.util.*;
import java.util.logging.*;
import de.tabuma.projectview.*;
import de.tabuma.pm.projectnavigation.entity.*;
import de.tabuma.pm.projectnavigation.logik.*;
import de.tabuma.ssoclient.entity.Privileg;
import de.tabuma.pm.design.*;

/**
 *
 * @author alefen
 */
public class CSSDropDownLinkStructurFacade {

    private static final Logger log = Logger.getLogger(CSSDropDownLinkStructurFacade.class.getName());

    /**
     * Creates a link structur in ribbon design.<br/>
     * ____________
     * | Link 1   |  Link2   Link3   Link4
     * |          |______________________________________
     * |                                                |
     * | ----------  ---------   ----------  ---------- |
     * | | Link11 |  | Link12 |  | Link13 |  | Link14 | |
     * | ----------  ----------  ----------  ---------- |
     * --------------------------------------------------
     * @param projectId
     * @return
     */

    public static String getLinkStructur(int projectId, String ssoId, String setting, String design, String current, int designId) {

        List<ProjectNavigation> list = ProjectNavigationFacade.getList(projectId);

        List<Design> dList = DesignFacade.getList(projectId);

        long maxPnId = 0;
        for(int i=0;i<list.size();i++) {
            if(maxPnId<list.get(i).getId().longValue()) {
                maxPnId = list.get(i).getId().longValue();
            }
        }
        maxPnId++;
        if(dList.size()>0) {
            ProjectNavigation pnSetting = new ProjectNavigation();
            pnSetting.setId(maxPnId);
            pnSetting.setNavMode(0);
            pnSetting.setName(setting);
            pnSetting.setId(maxPnId);
            pnSetting.setNavigationId(0);
            pnSetting.setOrder(list.size()+1);
            pnSetting.setProjectId(projectId);
            pnSetting.setFunctionTypeId(ProjectNavigation.TYPE_FOLDER);
            list.add(pnSetting);

            maxPnId++;
            ProjectNavigation pnDesign = new ProjectNavigation();
            pnDesign.setId(maxPnId);
            pnDesign.setNavMode(0);
            pnDesign.setName(design);
            pnDesign.setNavMode(0);
            pnDesign.setNavigationId(pnSetting.getId().intValue());
            pnDesign.setOrder(1);
            pnDesign.setFunctionTypeId(ProjectNavigation.TYPE_FOLDER);
            pnDesign.setProjectId(projectId);
            list.add(pnDesign);
            maxPnId++;

            for(int i=0;i<dList.size();i++) {
                Design d = dList.get(i);

                ProjectNavigation pnDesign1 = new ProjectNavigation();
                pnDesign1.setId(maxPnId);
                pnDesign1.setNavMode(0);
                pnDesign1.setNavigationId(pnDesign.getId().intValue());
                pnDesign1.setProjectId(projectId);
                pnDesign1.setFunctionTypeId(ProjectNavigation.TYPE_DESIGN);
                pnDesign1.setFunctionId(d.getId().intValue());
                pnDesign1.setOrder(i+1);
                if(d.getId().intValue()==designId) {
                    pnDesign1.setName(d.getName() + " ("+current+")");
                }
                else {
                    pnDesign1.setName(d.getName());

                }
                list.add(pnDesign1);
                maxPnId++;
            }

            if(ssoId!=null) {
                if(ssoId.equals("")) {
                    ProjectNavigation pnLogout = new ProjectNavigation();
                    pnLogout.setId(maxPnId);
                    pnLogout.setNavMode(0);
                    pnLogout.setName("Logout");
                    pnLogout.setNavMode(0);
                    pnLogout.setProjectId(projectId);
                    pnLogout.setNavigationId(pnSetting.getId().intValue());
                    pnLogout.setOrder(pnSetting.getId().intValue());
                    pnLogout.setFunctionTypeId(ProjectNavigation.TYPE_LOGOUT);
                    list.add(pnLogout);
                }
            }
        }
        else {
            if(ssoId!=null) {
                if(ssoId.equals("")) {
                    ProjectNavigation pnSetting = new ProjectNavigation();
                    pnSetting.setId(maxPnId);
                    pnSetting.setNavMode(0);
                    pnSetting.setName(setting);
                    pnSetting.setId(maxPnId);
                    pnSetting.setNavigationId(0);
                    pnSetting.setOrder(list.size()+1);
                    pnSetting.setProjectId(projectId);
                    pnSetting.setFunctionTypeId(ProjectNavigation.TYPE_FOLDER);
                    list.add(pnSetting);
                    maxPnId++;

                    ProjectNavigation pnLogout = new ProjectNavigation();
                    pnLogout.setId(maxPnId);
                    pnLogout.setNavMode(0);
                    pnLogout.setName("Logout");
                    pnLogout.setNavMode(0);
                    pnLogout.setNavigationId(pnSetting.getId().intValue());
                    pnLogout.setProjectId(projectId);
                    pnLogout.setOrder(pnSetting.getId().intValue());
                    pnLogout.setFunctionTypeId(ProjectNavigation.TYPE_LOGOUT);
                    list.add(pnLogout);
                }
            }
        }

        ProjectNavigation pn = new ProjectNavigation();
        ProjectNavigationWrapper pnw = new ProjectNavigationWrapper(pn, projectId);
        pnw.setNavigationId(0);
        pnw.setId(new Long(0));
        pnw.loadSubElement(list);


        List<de.tabuma.ssoclient.entity.Privileg> pList = null;
        if(ssoId!=null) {
            try {
                pList = de.tabuma.ssoclient.logik.SSOFacade.getUserPrivileges(ssoId);
            }
            catch(Exception e) {

            }
        }


        String str = "";

        str += "\t<div class=\"mainContainer\">\n";
        str += "\t\t<div id=\"myslidemenu\" class=\"jqueryslidemenu\">\n";

        //TODO Design Wechsel
        str += printNavigation(pnw,"\t\t\t", ssoId);

        str += "\t\t</div>\n";
        str += "\t</div>\n";
        return str;
    }

    public static String printNavigation(ProjectNavigationWrapper pnw, String ebene, String ssoId) {
        String str = ebene + "<ul>\n";
        for(int i=0;i<pnw.getSubElements().size();i++) {
            ProjectNavigationWrapper pnw1 = pnw.getSubElements().get(i);

            String link = "";
            if(pnw1.getNavMode()!=0) {
                link = " href=\"javascript:parent."+constants.functionName+pnw1.getId()+"()\" onclick=\"javascript:parent."+constants.functionName+pnw1.getId()+"()\"";
            }
            else {
                if(pnw1.getFunctionTypeId()==ProjectNavigation.TYPE_DESIGN) {
                    link = " href=\""+constants.projectview+"?"+constants.ssoId+"="+ssoId+"&"+constants.changeDesign+"="+pnw1.getFunctionId()+"&"+constants.projectId+"="+pnw.getProjectId()+"\"";
                }
                if(pnw1.getFunctionTypeId()==ProjectNavigation.TYPE_LOGOUT) {

                    link = " href=\""+constants.projectview+"?"+constants.ssoId+"="+ssoId+"&"+constants.logout+"=true&"+constants.projectId+"="+pnw.getProjectId()+"\"";
                }
            }

            if(pnw1.getSubElements().size()==0) {
                str += ebene + "<li><a"+link+">"+pnw1.getName()+"</a></li>\n";
            }
            if(pnw1.getSubElements().size()>0) {
                str += ebene + "<li><a"+link+">"+pnw1.getName()+"</a>\n";
                str += printNavigation(pnw1, ebene +"\t", ssoId);
                str += ebene + "</li>\n";
            }
        }
        str += "</ul>\n";
        return str;
    }

    public static String getRibbonName(String name) {
        if(name==null) {
            return "";
        }
        name = name.replaceAll(" ","_");
        return name;
    }

    public static List<ProjectNavigationWrapper> getLinkStructur(ProjectNavigationWrapper pnw) {
        List<ProjectNavigationWrapper> list = new ArrayList<ProjectNavigationWrapper>();
        for(int i=0;i<pnw.getSubElements().size();i++) {
            list.add(pnw.getSubElements().get(i));
            List<ProjectNavigationWrapper> myList = getLinkStructur(pnw.getSubElements().get(i));
            list.addAll(myList);
        }
        return list;
    }

    public static boolean userHasPrivileg(ProjectNavigation pn, List<Privileg> pList) {
        String privileg = de.tabuma.pm.constants.PROJECTNAVIGATION+" "+pn.getName()+" ("+pn.getId()+")";
        if(pList==null) {
            return false;
        }
        for(int i=0;i<pList.size();i++) {
            String privilegName = pList.get(i).getName();
            log.warning(privileg+"/"+privilegName);
            if(privilegName.equals(privileg)) {
                return true;
            }
        }
        return false;
    }
}